package com.ruoyi.ylxt.ypxx.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ylxt.ypxx.domain.Ypxx;
import com.ruoyi.ylxt.ypxx.service.IYpxxService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 药品信息Controller
 *
 * @author huacai
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/ylxt/ypxx")
public class YpxxController extends BaseController {
    @Resource
    private IYpxxService ypxxService;

    /**
     * 查询药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ypxx ypxx) {
        startPage();
        List<Ypxx> list = ypxxService.selectYpxxList(ypxx);
        return getDataTable(list);
    }

    /**
     * 导出药品信息列表
     */
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:export')")
    @Log(title = "药品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ypxx ypxx) {
        List<Ypxx> list = ypxxService.selectYpxxList(ypxx);
        ExcelUtil<Ypxx> util = new ExcelUtil<Ypxx>(Ypxx.class);
        util.exportExcel(response, list, "药品信息数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ypxx> util = new ExcelUtil<Ypxx>(Ypxx.class);
        util.importTemplateExcel(response, "药品信息数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "药品信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Ypxx> util = new ExcelUtil<Ypxx>(Ypxx.class);
        InputStream inputStream = file.getInputStream();
        List<Ypxx> list = util.importExcel(inputStream);
        inputStream.close();
        int count = ypxxService.batchInsertYpxx(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取药品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:query')")
    @GetMapping(value = "/{ypxxId}")
    public AjaxResult getInfo(@PathVariable("ypxxId") String ypxxId) {
        return success(ypxxService.selectYpxxByYpxxId(ypxxId));
    }

    /**
     * 新增药品信息
     */
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:add')")
    @Log(title = "药品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ypxx ypxx) {
        return toAjax(ypxxService.insertYpxx(ypxx));
    }

    /**
     * 修改药品信息
     */
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:edit')")
    @Log(title = "药品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ypxx ypxx) {
        return toAjax(ypxxService.updateYpxx(ypxx));
    }

    /**
     * 删除药品信息
     */
    @PreAuthorize("@ss.hasPermi('ylxt:ypxx:remove')")
    @Log(title = "药品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ypxxIds}")
    public AjaxResult remove(@PathVariable String[] ypxxIds) {
        return toAjax(ypxxService.deleteYpxxByYpxxIds(ypxxIds));
    }
}
